package _09练习;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CalculatorFrame extends JFrame {

	private JPanel contentPane;
	private JTextField num1Field;
	private JTextField num2Field;
	
	JLabel resultLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CalculatorFrame frame = new CalculatorFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CalculatorFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("+");
		lblNewLabel.setBounds(95, 60, 54, 15);
		contentPane.add(lblNewLabel);
		
		num1Field = new JTextField();
		num1Field.setBounds(10, 57, 66, 21);
		contentPane.add(num1Field);
		num1Field.setColumns(10);
		
		num2Field = new JTextField();
		num2Field.setBounds(145, 57, 66, 21);
		contentPane.add(num2Field);
		num2Field.setColumns(10);
		
		resultLabel = new JLabel("");
		resultLabel.setBounds(342, 60, 54, 15);
		contentPane.add(resultLabel);
		
		
		JButton btnNewButton = new JButton("=");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				// 1. 获取到用户输入的信息
				double num1 = Double.parseDouble(num1Field.getText());
				double num2 = Double.parseDouble(num2Field.getText());
				
				// 2. 计算
				double result = num1 + num2;
				
				// 3. 结果显示
				resultLabel.setText(""+result);
				
			}
		});
		btnNewButton.setBounds(238, 56, 93, 23);
		contentPane.add(btnNewButton);
		
		

	
	}

}
